package com.hx.setting.action.interceptor;

import com.hx.setting.action.BaseAction;
import com.hx.setting.action.request.BaseActionRequest;
import com.hx.setting.utils.TimeUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
public class MountParameterInterceptor extends AbstractInterceptor
{

	/**  */
	private static final long								serialVersionUID	= -52077754451544839L;

	private static Map<String, Map<String, List<Method>>>	METHOD_MAP			= new HashMap<String, Map<String, List<Method>>>();

	@Override
	public String intercept(ActionInvocation invocation) throws Exception
	{
		Object action = invocation.getAction();
		if ( action instanceof BaseAction )
		{
			ActionContext actionContext = invocation.getInvocationContext();
			HttpServletRequest req = (HttpServletRequest) actionContext.get( StrutsStatics.HTTP_REQUEST );
			BaseAction baseAction = (BaseAction) action;
			BaseActionRequest request = baseAction.getActionRequest();
			Map<String, List<Method>> map = METHOD_MAP.get( request.getClass().getName() );
			if ( map == null )
			{
				map = initMethod( request );
				METHOD_MAP.put( request.getClass().getName() ,map );
			}
			Enumeration<String> e = req.getParameterNames();
			String k = null;
			String v = null;
			List<Method> methods = null;
			while ( e.hasMoreElements() )
			{
				k = e.nextElement();
				v = req.getParameter( k );
				if ( v != null && v.length() > 0 )
				{
					methods = map.get( getSetterMethodName( k ) );
					if ( methods != null )
					{
						for ( Method method : methods )
						{
							try
							{
								Object o = getObject( method ,v );
								method.invoke( request ,o );
							}
							catch ( Exception e1 )
							{
								//e1.printStackTrace();
							}
							break;
						}
					}
				}
			}
		}
		return invocation.invoke();
	}

	private final Map<String, List<Method>> initMethod(Object obj)
	{
		Map<String, List<Method>> map = new HashMap<String, List<Method>>();
		Class<?> clazz = obj.getClass();
		while ( clazz != null && clazz != Object.class )
		{
			Method[] methods = clazz.getDeclaredMethods();
			String name = null;
			for ( Method method : methods )
			{
				name = method.getName();
				if ( name.startsWith( "set" ) )
				{
					List<Method> ml = map.get( name );
					if ( ml == null )
					{
						ml = new ArrayList<Method>();
						map.put( name ,ml );
					}
					ml.add( method );
				}
			}
			clazz = clazz.getSuperclass();
		}
		return map;
	}

	private final Object getObject(Method method, String value) throws Exception
	{
		Object answer = null;
		Class<?> clazz[] = method.getParameterTypes();
		if ( clazz != null && clazz.length > 0 )
		{
			Class<?> type = clazz[0];
			if ( String.class.equals( type ) )
			{
				answer = value;
			}
			else if ( Long.class.equals( type ) || long.class.equals( type ) )
			{
				answer = Long.parseLong( value );
			}
			else if ( Integer.class.equals( type ) || int.class.equals( type ) )
			{
				answer = Integer.parseInt( value );
			}
			else if ( Date.class.equals( type ) )
			{
				Date d = TimeUtil.parse( value ,TimeUtil.FORMAT_NORMAL );
				if ( d == null )
				{
					d = TimeUtil.parse( value ,TimeUtil.FORMAT_DATE_ONLY );
				}
				answer = d;
			}
			else if ( Double.class.equals( type ) || double.class.equals( type ) )
			{
				answer = Double.parseDouble( value );
			}
			else if ( Character.class.equals( type ) || char.class.equals( type ) )
			{
				answer = value.charAt( 0 );
			}
			else if ( Float.class.equals( type ) || float.class.equals( type ) )
			{
				answer = Float.parseFloat( value );
			}
			else if ( Short.class.equals( type ) || short.class.equals( type ) )
			{
				answer = Short.parseShort( value );
			}
			else if ( Boolean.class.equals( type ) || boolean.class.equals( type ) )
			{
				boolean bool = false;
				char c = value.charAt( 0 );
				if ( 't' == c || '1' == c || 'T' == c )
				{
					bool = true;
				}
				answer = bool;
			}
			else if ( BigDecimal.class.equals( type ) )
			{
				answer = new BigDecimal( value );
			}
			else
			{
				answer = value;
			}
		}
		return answer;
	}

	private final String getSetterMethodName(String javaProperty)
	{
		StringBuilder answer = new StringBuilder( "set" );
		answer.append( javaProperty );
		if ( Character.isLowerCase( answer.charAt( 3 ) ) )
		{
			answer.setCharAt( 3 ,Character.toUpperCase( answer.charAt( 3 ) ) );
		}
		return answer.toString();
	}
}
/* Honey v3.1 Test - version:1 - OriginalChecksum:B8fb48dd659f8b05088cc2af043d96bfd (do not edit this line) */
